package com.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.enmu.DeletedEnum;
import com.product.enmu.OperatorType;
import com.product.enmu.OrderState;
import com.product.entity.OperationLog;
import com.product.entity.Orders;
import com.product.mapper.OrdersMapper;
import com.product.service.IOperationLogService;
import com.product.service.IOrderItemService;
import com.product.service.IOrdersService;
import com.product.to.AddOrderTo;
import com.product.to.UpdateOrderTo;
import com.product.utils.PageUtils;
import com.product.vo.OrderAndOrderItemVo;
import com.product.vo.OrderItemVo;
import com.product.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {
    @Autowired
    private IOperationLogService operatorService;
    @Autowired
    private IOrderItemService orderItemService;

    @Override
    public void deleteOrders(List<Long> orderIds) {
        for (Long productId : orderIds) {
            deleteOrder(productId);
        }
    }

    @Override
    public void deleteOrder(Long orderId) {
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<Orders>()
                .eq(Orders::getOrderId, orderId)
                .set(Orders::getDeleted, DeletedEnum.DELETE.getValue());
        this.baseMapper.update(updateWrapper);

        operatorService.addOperation(OperatorType.Delete.getCode(), "删除Orders表");
    }

    @Override
    public void updateOrder(UpdateOrderTo order) {
        Orders newOrder = new Orders();
        BeanUtils.copyProperties(order, newOrder);
        this.baseMapper.updateById(newOrder);

        operatorService.addOperation(OperatorType.Update.getCode(), "修改Orders表");
    }

    @Override
    public void addOrder(AddOrderTo order) {
        Orders newOrders = new Orders();
        BeanUtils.copyProperties(order, newOrders);
        newOrders.setState(OrderState.WAIT.getState() + "");
        this.baseMapper.insert(newOrders);

        operatorService.addOperation(OperatorType.Insert.getCode(), "添加Orders表");
    }

    @Override
    public PageUtils getAllOrder(Integer size, Integer current, Integer orderState, String customerName, Long orderId,Integer timeOrderBy) {
        Page<Orders> page = getOrder(size, current, orderState, customerName, orderId,timeOrderBy);

        List<OrderVo> orderVoList = page.getRecords().stream().map(item -> {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(item, orderVo);
            return orderVo;
        }).toList();
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(orderVoList);
        return pageUtils;
    }

    @Override
    public PageUtils getAllOrderByItem(Integer size, Integer current, Integer orderState, String customerName, Long orderId,Integer timeOrderBy) {
        Page<Orders> page = getOrder(size, current, orderState, customerName, orderId,timeOrderBy);

        List<OrderAndOrderItemVo> orderVoList = page.getRecords().stream().map(item -> {
            OrderAndOrderItemVo orderVo = new OrderAndOrderItemVo();
            BeanUtils.copyProperties(item, orderVo);
            List<OrderItemVo> itemVos = orderItemService.getOrderById(orderVo.getOrderId()).stream().map(va -> {
                OrderItemVo itemVo = new OrderItemVo();
                BeanUtils.copyProperties(va, itemVo);
                return itemVo;
            }).toList();

            orderVo.setOrderItems(itemVos);
            return orderVo;
        }).toList();

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(orderVoList);
        return pageUtils;
    }

    private Page<Orders> getOrder(Integer size, Integer current, Integer orderState, String customerName, Long orderId,Integer timeOrderBy) {
        Page<Orders> orderPage = new Page<>(current, size);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(orderState != null, Orders::getState, orderState);
        queryWrapper.eq(orderId != null, Orders::getOrderId, orderId);
        queryWrapper.like(customerName != null, Orders::getCustomerName, customerName);
        queryWrapper.orderBy(timeOrderBy != null, timeOrderBy != null && timeOrderBy == 0, Orders::getCreationTime);
        return this.baseMapper.selectPage(orderPage, queryWrapper);
    }
}
